package Util;

import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class jFrame3 extends JFrame {
	public
	JPanel cmainpanel;
	JPanel cpanel1;
	JPanel cpanel2;
	JPanel cpanel3;
	JPanel cpanel4;
	JButton cseachButton;
	JButton cinButton;
	JButton cuButton;
	JButton cuButton1;
	JButton cchangelvButton6;
	JButton cchangedisButton7;
	JTextField inCField1;
	JTextField inCField2;
	JTextField inCField3;
	JTextField inCField4;
	JTextField inCField5;
	JTextField inCField6;
	JTextField inCField7;
	public jFrame3(){
		LayoutManager  bookview = new GridLayout(4,1);
        setLayout(bookview);
        setTitle("书店销售系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400, 400, 485, 343);
		setLocationRelativeTo(null);
		cmainpanel= new JPanel(new GridLayout(4,1));
		cpanel1 = new JPanel();
		cpanel2 = new JPanel();
		cpanel3 = new JPanel();
		cpanel4 = new JPanel();
		cseachButton = new JButton("会员信息");
		
		cseachButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stubtry (java.sql.Connection conn = Myutil.getConnection()){

				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String sql="select * from  customer ";
	                ResultSet rs=st.executeQuery(sql);
	                while(rs.next()) {
	    				String cnum =rs.getString("cnum");
	    				String  cname=rs.getString("cname");
	    				String  clevel =rs.getString("clevel");
	    				String  expense =rs.getString("expense");
	    				String  discount =rs.getString("discount");
	    				System.out.println(cnum+"--"+cname+"--"+clevel+"--"+expense+"--"+discount);
	    			}




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
				
			}
			
			
		});
		cinButton = new JButton("会员添加");
		cinButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                PreparedStatement ps = null;
	                String cnum =  inCField1.getText();
	                String cname= inCField2.getText();
	                String clevel= inCField3.getText();
	                String expense = inCField4.getText();
	                String discount =inCField5.getText();
	                
	                String sql= "insert into  customer values(?,?,?,?,?)";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, cnum);
	                ps.setString(2, cname);
	                ps.setString(3, clevel);
	                ps.setString(4, expense);
	                ps.setString(5, discount);
	               
	                ResultSet rs=ps.executeQuery();
	                JOptionPane.showMessageDialog(null,"添加成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);

	                
	                
	                
	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
				
			}
			});
		cuButton = new JButton("按照会员号删除");
	 
		cuButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                PreparedStatement ps = null;
	                String cnum =  inCField6.getText();
	                if(inCField6.getText().equals("")){
	                    JOptionPane.showMessageDialog(null,"会员号不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                else if(!inCField6.getText().equals("")){
	                String sql= "delete from customer where cnum=?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, cnum);
	                ResultSet rs=ps.executeQuery();
	                JOptionPane.showMessageDialog(null,"删除成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                }
	                
	                
	                
	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
			}
			
		});
		cuButton1 = new JButton("按照会员名删除");
		 
		cuButton1.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                PreparedStatement ps = null;
	                String cname =  inCField7.getText();
	                if(inCField7.getText().equals("")){
	                    JOptionPane.showMessageDialog(null,"会员名不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                else if(!inCField7.getText().equals("")){
	                String sql= "delete from customer where cname=?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, cname);
	                ResultSet rs=ps.executeQuery();
	                JOptionPane.showMessageDialog(null,"删除成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                }
	                
	                
	                
	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
			}
			
		});
		
		inCField1=new JTextField(8);
		 inCField2=new JTextField(8);
		 inCField3=new JTextField(8);
		 inCField4=new JTextField(8);
		 inCField5=new JTextField(8);
		 inCField6=new JTextField(8);
		 inCField7=new JTextField(8);
		cpanel1.add(cseachButton);
		cpanel2.add(cinButton);
		cpanel3.add(cuButton);
		cpanel4.add(cuButton1);
		cpanel2.add(inCField1);
		cpanel2.add(inCField2);
		cpanel2.add(inCField3);
		cpanel2.add(inCField4);
		cpanel2.add(inCField5);
		cpanel3.add(inCField6);
		cpanel4.add(inCField7);
		add(cpanel1);
		add(cpanel2);
		add(cpanel3);
		add(cpanel4);
		setVisible(true);
	}
	 
	
	
	

}
